package com.da.lon.wang.xlg.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.activity.ProductsActivity;
import com.da.lon.wang.xlg.adapter.GroupBuyAdapter;
import com.da.lon.wang.xlg.adapter.SingleSaleAdapter;
import com.da.lon.wang.xlg.bean.AdBean;
import com.da.lon.wang.xlg.bean.AdListBean;
import com.da.lon.wang.xlg.bean.Goods;
import com.da.lon.wang.xlg.bean.ListBean;
import com.da.lon.wang.xlg.bean.SingleSaleGoods;
import com.da.lon.wang.xlg.bean.SingleSaleListBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase.Mode;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.da.lon.wang.xlg.pulltorefresh.library.PullToRefreshListView;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;
import java.util.List;

public class SingleSaleFragment extends BaseFragment implements
        OnRefreshListener2<ListView>, OnCheckedChangeListener {

    private PullToRefreshListView listView;
    private PullToRefreshListView listView2;
    private RadioGroup buyerGroup;
    private ImageView headerImageView;
    private int page;
    private List<SingleSaleGoods> list = new ArrayList<SingleSaleGoods>();
    private List<Goods> list2 = new ArrayList<Goods>();
    private List<AdBean> list3 = new ArrayList<AdBean>();
    private Context context;
    private View view;

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SingleSaleFragment");
        MobclickAgent.onResume(getActivity());
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SingleSaleFragment");
        MobclickAgent.onPause(getActivity());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    protected View inflateView(LayoutInflater inflater, ViewGroup var2) {
        // TODO Auto-generated method stub
        view = inflater.inflate(R.layout.ouka_buyer_singlesale_fragment, null);
        return view;

    }

    @Override
    protected void initView() {
        // TODO Auto-generated method stub
        context = getActivity();
        initViews(view);

    }

    private void initViews(View view) {
        try {
            // TODO Auto-generated method stub
            View view1 = LayoutInflater.from(getActivity()).inflate(
                    R.layout.ouka_buyer_singlesale_listviewitem, null);
            listView = (PullToRefreshListView) view
                    .findViewById(R.id.ouka_buy_listview);
            listView2 = (PullToRefreshListView) view
                    .findViewById(R.id.ouka_buy2_listview);

            headerImageView = (ImageView) view1
                    .findViewById(R.id.ouka_single_sale_listview_ad);

            buyerGroup = (RadioGroup) view.findViewById(R.id.buyer_home_rg);
            buyerGroup.setOnCheckedChangeListener(this);
            buyerGroup
                    .setBackgroundResource(R.drawable.ouka_home_buyer_cheked1);

            listView.getRefreshableView().addHeaderView(headerImageView);
            listView.setMode(Mode.BOTH);
            listView.setOnRefreshListener(this);

            listView2.setMode(Mode.BOTH);
            listView2.setOnRefreshListener(this);

            listView2.getRefreshableView().setOnItemClickListener(
                    new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                                View view, int position, long id) {
                            // TODO Auto-generated method stub
                            if (parent.getAdapter() != null) {
                                Goods bean = (Goods) parent.getAdapter()
                                        .getItem(position);

                                PreferenceUtils.getInstance().SetSettingString(
                                        PreferencesConfig.PRODUCT_NUMBER,
                                        bean.getProduct_number());

                                Intent intent = new Intent(getActivity(),
                                        ProductsActivity.class);
                                startActivity(intent);
                            }
                        }
                    });

            listView.getRefreshableView().setOnItemClickListener(
                    new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                                View view, int position, long id) {
                            // TODO Auto-generated method stub

                            if (position == 1 && list3 != null
                                    && list3.size() > 0) {

                                AdBean bean = list3.get(0);

                                PreferenceUtils.getInstance().SetSettingString(
                                        PreferencesConfig.PRODUCT_NUMBER,
                                        bean.product_info.product_number);

                                Intent intent = new Intent(getActivity(),
                                        ProductsActivity.class);
                                startActivity(intent);

                            } else if (parent.getAdapter() != null) {

                                SingleSaleGoods bean = (SingleSaleGoods) parent
                                        .getAdapter().getItem(position);

                                PreferenceUtils.getInstance().SetSettingString(
                                        PreferencesConfig.PRODUCT_NUMBER,
                                        bean.getProduct_number());

                                Intent intent = new Intent(getActivity(),
                                        ProductsActivity.class);
                                startActivity(intent);
                            }
                        }
                    });

            HttpLoader<AdListBean> httpLoader = new HttpLoader<AdListBean>(
                    getActivity(), "http://okapi.hhecz.com/Product-pads",
                    AdListBean.class, 60 * 1000);
            AjaxParams params1 = new AjaxParams();
            params1.put("token", XLGDataUtils.encrypt2MD5("Product" + "2015#$%"
                    + "pads"));

            httpLoader.setManagerListener(new OnWebLoadListener<AdListBean>() {

                @Override
                public void OnStart() {
                    // TODO Auto-generated method stub
                    showDialog();
                }

                @Override
                public void OnError(String error) {
                    // TODO Auto-generated method stub
                    dismissDialog();
                    Toast.makeText(getActivity(), "加载失败", Toast.LENGTH_SHORT)
                            .show();
                }

                @Override
                public void onSuccess(AdListBean t) {
                    // TODO Auto-generated method stub
                    dismissDialog();

                    if (t != null && t.getCode().equals("000000")
                            && t.getResult() != null
                            && t.getResult().size() > 0
                            && t.getResult().size() > 0
                            && t.getResult().get(0) != null
                            && t.getResult().get(0).getImg_url() != null
                            && t.getResult().get(0).getLink() != null
                            && headerImageView != null) {
                        list3 = t.getResult();
                        headerImageView.setScaleType(ScaleType.CENTER_CROP);
                        // MyApplication.loadImage(context, t.getResult().get(0)
                        // .getImg_url(), headerImageView);
                        MyApplication.loadImageByPicasso(context, t.getResult()
                                        .get(0).getImg_url(), null, headerImageView, 0,
                                0, 0);
                        PreferenceUtils.getInstance().SetSettingString(
                                PreferencesConfig.AD_LINK,
                                t.getResult().get(0).getLink());
                    }
                }
            });
            httpLoader.loadData(params1);

            HttpLoader<SingleSaleListBean> loader = new HttpLoader<SingleSaleListBean>(
                    getActivity(), "http://okapi.hhecz.com/Product-plist",
                    SingleSaleListBean.class, 60 * 1000);
            AjaxParams params = new AjaxParams();
            params.put("page", page + "");

            params.put("token", XLGDataUtils.encrypt2MD5("Product" + "2015#$%"
                    + "plist"));

            loader.setManagerListener(new OnWebLoadListener<SingleSaleListBean>() {

                @Override
                public void OnStart() {
                    // TODO Auto-generated method stub
                    showDialog();
                }

                @Override
                public void OnError(String error) {
                    // TODO Auto-generated method stub
                    dismissDialog();
                }

                @Override
                public void onSuccess(SingleSaleListBean t) {
                    // TODO Auto-generated method stub
                    dismissDialog();

                    SingleSaleListBean bean = (SingleSaleListBean) t;
                    if (t != null && bean.getCode().equals("000000")) {
                        list = t.getResult();
                        if (list != null && listView != null
                                && listView.getRefreshableView() != null)
                            listView.getRefreshableView().setAdapter(
                                    new SingleSaleAdapter(list, getActivity()));
                    }
                }
            });
            loader.loadData(params);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        try {
            page = 1;
            HttpLoader<SingleSaleListBean> loader = new HttpLoader<SingleSaleListBean>(
                    getActivity(), "okapi.hhecz.com/Product-plist",
                    SingleSaleListBean.class, 60 * 1000);
            AjaxParams params = new AjaxParams();
            params.put("page", page + "");

            params.put("token", XLGDataUtils.encrypt2MD5("Product" + "2015#$%"
                    + "plist"));
            loader.setManagerListener(new OnWebLoadListener<SingleSaleListBean>() {

                @Override
                public void OnStart() {
                    // TODO Auto-generated method stub
                    showDialog();
                }

                @Override
                public void OnError(String error) {
                    // TODO Auto-generated method stub
                    dismissDialog();
                    new Handler().post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (listView != null) {
                                listView.onRefreshComplete();
                            }
                        }
                    });
                }

                @Override
                public void onSuccess(SingleSaleListBean t) {
                    // TODO Auto-generated method stub
                    dismissDialog();
                    new Handler().post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (listView != null) {
                                listView.onRefreshComplete();
                            }
                        }
                    });
                    if (t != null) {
                        SingleSaleListBean bean = (SingleSaleListBean) t;
                        if (bean != null && bean.getCode().equals("000000")) {
                            if (list != null) {
                                list.clear();
                            }
                            list = t.getResult();
                            if (list != null && listView != null
                                    && listView.getRefreshableView() != null) {
                                listView.getRefreshableView().setAdapter(
                                        new SingleSaleAdapter(list,
                                                getActivity()));
                            }
                        }
                    }
                }
            });
            loader.loadData(params);

            HttpLoader<ListBean> loader1 = new HttpLoader<ListBean>(
                    getActivity(), "http://okapi.hhecz.com/Product-pgourp",
                    ListBean.class, 60 * 1000);
            AjaxParams params1 = new AjaxParams();
            params1.put("page", page + "");

            params1.put("token", XLGDataUtils.encrypt2MD5("Product" + "2015#$%"
                    + "pgourp"));
            loader1.setManagerListener(new OnWebLoadListener<ListBean>() {

                @Override
                public void OnStart() {
                    // TODO Auto-generated method stub
                    showDialog();
                }

                @Override
                public void OnError(String error) {
                    // TODO Auto-generated method stub
                    dismissDialog();
                    new Handler().post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (listView2 != null) {
                                listView2.onRefreshComplete();
                            }
                        }
                    });
                }

                @Override
                public void onSuccess(ListBean t) {
                    // TODO Auto-generated method stub
                    dismissDialog();
                    new Handler().post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (listView2 != null) {
                                listView2.onRefreshComplete();
                            }
                        }
                    });
                    if (t != null) {
                        ListBean bean = (ListBean) t;
                        if (bean != null && bean.getCode().equals("000000")) {
                            if (list2 != null) {
                                list2.clear();
                            }
                            list2 = t.getResult();
                            if (list2 != null && listView2 != null
                                    && listView2.getRefreshableView() != null)
                                listView2.getRefreshableView().setAdapter(
                                        new GroupBuyAdapter(list2,
                                                getActivity()));
                        }
                    }
                }
            });
            loader1.loadData(params1);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        try {
            page++;
            HttpLoader<SingleSaleListBean> loader = new HttpLoader<SingleSaleListBean>(
                    getActivity(), "http://okapi.hhecz.com/Product-plist",
                    SingleSaleListBean.class, 60 * 1000);
            AjaxParams params = new AjaxParams();
            params.put("page", page + "");

            params.put("token", XLGDataUtils.encrypt2MD5("Product" + "2015#$%"
                    + "plist"));
            loader.setManagerListener(new OnWebLoadListener<SingleSaleListBean>() {

                @Override
                public void OnStart() {
                    showDialog();
                }

                @Override
                public void OnError(String error) {
                    dismissDialog();
                    new Handler().post(new Runnable() {

                        @Override
                        public void run() {
                            if (listView != null)
                                listView.onRefreshComplete();
                        }
                    });
                }

                @Override
                public void onSuccess(SingleSaleListBean t) {
                    // TODO Auto-generated method stub
                    dismissDialog();
                    new Handler().post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (listView != null)
                                listView.onRefreshComplete();
                        }
                    });
                    if (t != null) {
                        SingleSaleListBean bean = (SingleSaleListBean) t;
                        if (bean != null && bean.getCode().equals("000000")) {
                            if (t.getResult() == null && list != null) {
                                SingleSaleAdapter adapter = new SingleSaleAdapter(
                                        list, getActivity());
                                adapter.notifyDataSetChanged();
                            } else {
                                list = t.getResult();
                                if (list != null) {
                                    SingleSaleAdapter adapter = new SingleSaleAdapter(
                                            list, getActivity());
                                    adapter.notifyDataSetChanged();
                                }
                            }
                        }
                    }
                }
            });
            loader.loadData(params);

            HttpLoader<ListBean> loader1 = new HttpLoader<ListBean>(
                    getActivity(), "http://okapi.hhecz.com/Product-pgourp",
                    ListBean.class, 60 * 1000);
            AjaxParams params1 = new AjaxParams();
            params1.put("page", page + "");

            params1.put("token", XLGDataUtils.encrypt2MD5("Product" + "2015#$%"
                    + "pgourp"));
            loader1.setManagerListener(new OnWebLoadListener<ListBean>() {

                @Override
                public void OnStart() {
                    // TODO Auto-generated method stub
                    showDialog();
                }

                @Override
                public void OnError(String error) {
                    // TODO Auto-generated method stub
                    dismissDialog();
                    new Handler().post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (listView2 != null)
                                listView2.onRefreshComplete();
                        }
                    });
                }

                @Override
                public void onSuccess(ListBean t) {
                    // TODO Auto-generated method stub
                    dismissDialog();
                    new Handler().post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (listView2 != null)
                                listView2.onRefreshComplete();
                        }
                    });
                    if (t != null) {
                        ListBean bean = (ListBean) t;
                        if (bean != null && bean.getCode().equals("000000")) {
                            if (t.getResult() == null && list2 != null) {
                                GroupBuyAdapter adapter = new GroupBuyAdapter(
                                        list2, getActivity());
                                adapter.notifyDataSetChanged();
                            } else {
                                list2 = t.getResult();
                                if (list2 != null) {
                                    GroupBuyAdapter adapter = new GroupBuyAdapter(
                                            list2, getActivity());
                                    adapter.notifyDataSetChanged();
                                }
                            }
                        }
                    }
                }
            });
            loader1.loadData(params1);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub

        switch (checkedId) {
            case R.id.single_sale:
                try {
                    page = 1;

                    listView.setVisibility(View.VISIBLE);
                    listView.setPadding(0, 0, 0, 0);
                    listView2.setVisibility(View.GONE);

                    headerImageView.setVisibility(View.VISIBLE);
                    headerImageView.setPadding(0, 0, 0, 0);

                    HttpLoader<SingleSaleListBean> loader = new HttpLoader<SingleSaleListBean>(
                            getActivity(), "http://okapi.hhecz.com/Product-plist",
                            SingleSaleListBean.class, 60 * 1000);
                    AjaxParams params = new AjaxParams();
                    params.put("page", page + "");

                    params.put("token", XLGDataUtils.encrypt2MD5("Product"
                            + "2015#$%" + "plist"));
                    loader.setManagerListener(new OnWebLoadListener<SingleSaleListBean>() {

                        @Override
                        public void OnStart() {
                            // TODO Auto-generated method stub
                            showDialog();
                        }

                        @Override
                        public void OnError(String error) {
                            // TODO Auto-generated method stub
                            dismissDialog();
                        }

                        @Override
                        public void onSuccess(SingleSaleListBean t) {
                            // TODO Auto-generated method stub
                            dismissDialog();
                            if (t != null) {
                                SingleSaleListBean bean = (SingleSaleListBean) t;
                                if (bean != null && bean.getCode().equals("000000")) {
                                    list = t.getResult();
                                    if (list != null)
                                        listView.getRefreshableView().setAdapter(
                                                new SingleSaleAdapter(list,
                                                        getActivity()));
                                }
                            }
                        }
                    });
                    loader.loadData(params);

                    buyerGroup
                            .setBackgroundResource(R.drawable.ouka_home_buyer_cheked1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case R.id.group_buy:
                try {
                    page = 1;

                    listView.setVisibility(View.GONE);
                    listView2.setVisibility(View.VISIBLE);
                    listView2.setPadding(0, 0, 0, 0);

                    headerImageView.setVisibility(View.GONE);
                    headerImageView.setPadding(0, -headerImageView.getHeight(), 0,
                            0);

                    listView.setVisibility(View.GONE);
                    listView2.setVisibility(View.VISIBLE);
                    listView2.setPadding(0, 0, 0, 0);

                    HttpLoader<ListBean> loader1 = new HttpLoader<ListBean>(
                            getActivity(), "http://okapi.hhecz.com/Product-pgourp",
                            ListBean.class, 60 * 1000);
                    AjaxParams params1 = new AjaxParams();
                    params1.put("page", page + "");

                    params1.put("token", XLGDataUtils.encrypt2MD5("Product"
                            + "2015#$%" + "pgourp"));
                    loader1.setManagerListener(new OnWebLoadListener<ListBean>() {

                        @Override
                        public void OnStart() {
                            // TODO Auto-generated method stub
                            showDialog();
                        }

                        @Override
                        public void OnError(String error) {
                            // TODO Auto-generated method stub
                            dismissDialog();
                        }

                        @Override
                        public void onSuccess(ListBean t) {
                            // TODO Auto-generated method stub
                            dismissDialog();
                            if (t != null) {
                                ListBean bean = (ListBean) t;
                                if (bean != null && bean.getCode().equals("000000")) {
                                    list2 = t.getResult();
                                    if (list2 != null)
                                        listView2.getRefreshableView().setAdapter(
                                                new GroupBuyAdapter(list2,
                                                        getActivity()));
                                }
                            }
                        }
                    });

                    loader1.loadData(params1);

                    buyerGroup
                            .setBackgroundResource(R.drawable.ouka_home_buyer_checked2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            default:
                break;
        }
    }

    @Override
    protected void RefreshData() {
        // TODO Auto-generated method stub

    }

}
